package com.foxchen.textinputapp.utils;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
@Slf4j
public class I18nUti {
    @Resource
    private MessageSource messageSource;
    private static MessageSource MESSAGE_SOURCE;

    @PostConstruct
    public void setup(){
        MESSAGE_SOURCE = messageSource;
    }

    public static String getStrByKey(String key) {
        Locale locale = LocaleContextHolder.getLocale();
        return MESSAGE_SOURCE.getMessage(key,null,locale);
    }

    public static String getStrByKey(String key,Object... params) {
        Locale locale = LocaleContextHolder.getLocale();

        return MESSAGE_SOURCE.getMessage(key,params,locale);
    }
}
